package com.cysyz.mylib.excel.engine.impl.poi2;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.cysyz.mylib.excel.engine.CellEngine;
import com.cysyz.mylib.excel.engine.SheetEngine;

public class SheetEnginePoi2Impl implements SheetEngine{
	private HSSFWorkbook workbook;
	private int sheetIndex;
	private HSSFSheet sheet;
	public SheetEnginePoi2Impl(HSSFWorkbook workbook, HSSFSheet sheet, int sheetIndex) {
		super();
		this.workbook = workbook;
		this.sheetIndex = sheetIndex;
		this.sheet = sheet;
	}

	@Override
	public String getName() {
		return workbook.getSheetName(sheetIndex);
	}

	@Override
	public List<CellEngine> getOneRowCells(int rowIndex) {
		HSSFRow row = sheet.getRow(rowIndex);
		List<CellEngine> cells = null;
		if (row != null) {
			int cellCount = sheet.getRow(rowIndex).getPhysicalNumberOfCells();
			cells = new ArrayList<CellEngine>(); 
			for (short cellnum = 0 ; cellnum < cellCount ; cellnum ++) {
				cells.add(new CellEnginePoi2Impl(sheet.getRow(rowIndex).getCell(cellnum)));
			}
		}
		return cells;
	}

	@Override
	public int getRowCount() {
		return sheet.getPhysicalNumberOfRows();
	}

}
